package com.cykj.faceledtv.ui.base;


import com.cykj.faceledtv.data.DataManager;
import com.cykj.faceledtv.error.ANError;
import com.cykj.faceledtv.network.ExceptionHandle;
import com.orhanobut.logger.Logger;

import javax.inject.Inject;

import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.functions.Consumer;

/**
 * P层基类
 *
 * @author jzh
 * @date 2018/12/13 09:03
 */
public class BasePresenter<V extends MvpView> implements MvpPresenter<V> {

    private V mMvpView;

    private final DataManager mDataManager;

    private final CompositeDisposable mCompositeDisposable;


    @Inject
    public BasePresenter(DataManager dataManager, CompositeDisposable compositeDisposable) {
        mDataManager = dataManager;
        mCompositeDisposable = compositeDisposable;
    }


    @Override
    public void onAttach(V mvpView) {
        mMvpView = mvpView;
    }

    @Override
    public void onDetach() {
        mCompositeDisposable.dispose();
        mMvpView = null;
    }

    /**
     * 判null 防止出现null 异常
     */
    public boolean isViewAttached() {
        return mMvpView != null;
    }

    public V getMvpView() {
        return mMvpView;
    }

    /**
     * 检查view是否初始化
     */
    public void checkViewAttached() {
        if (!isViewAttached()) {
            throw new MvpViewNotAttachedException();
        }
    }

    public DataManager getDataManager() {
        return mDataManager;
    }

    public CompositeDisposable getCompositeDisposable() {
        return mCompositeDisposable;
    }

    @Override
    public void handleApiError(String msg) {
        Logger.e("network error=" + msg);
    }

    @Override
    public void handleApiANError(ANError error) {
        //处理 请求具体错误
    }


    private static class MvpViewNotAttachedException extends RuntimeException {
        MvpViewNotAttachedException() {
            super("Please call Presenter.onAttach(MvpView) before" +
                    " requesting data to the Presenter");
        }
    }

    /**
     * 默认的处理异常的方法 隐藏所有dialog并显示提示信息
     *
     * @param isClick 是否显示消息为 xxx 点击重试
     * @return consumer
     */
    public Consumer<Throwable> getConsumer(final boolean isClick) {
        return new Consumer<Throwable>() {
            @Override
            public void accept(@NonNull Throwable throwable) throws Exception {
                throwable.printStackTrace();
                if (!isViewAttached()) {
                    return;
                }
                String msg = ExceptionHandle.handleException(throwable, isClick);
                Logger.e("error=" + msg);
            }
        };
    }


    /**
     * 得到返回的消息到handleApiError方法上 具体操作交给该方法实现
     *
     * @param isClick 是否显示消息为 xxx 点击重试
     * @return consumer
     */
    public Consumer<Throwable> getHandleConsumer(final boolean isClick) {
        return new Consumer<Throwable>() {
            @Override
            public void accept(@NonNull Throwable throwable) throws Exception {
                throwable.printStackTrace();
                if (!isViewAttached()) {
                    return;
                }
                String msg = ExceptionHandle.handleException(throwable, isClick);
                handleApiError(msg);
            }
        };
    }


}
